package com.baizhi.controller;


import com.baizhi.dto.AttrDto;
import com.baizhi.dto.SpuAttrDto;
import com.baizhi.entity.Attr;
import com.baizhi.entity.SpuAttrValue;
import com.baizhi.entity.SpuInfo;
import com.baizhi.service.IAttrService;
import com.baizhi.service.ISpuAttrValueService;
import com.baizhi.service.ISpuInfoService;
import com.baizhi.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * spu属性值 前端控制器
 * </p>
 *
 * @author huiJie
 * @since 2021-08-31
 */
@RestController
@RequestMapping("/spuAttrValue")
public class SpuAttrValueController {
    @Autowired
    private ISpuAttrValueService spuAttrValueService;
    @Autowired
    private IAttrService attrService;
    @PostMapping
    public R addAttrValue(@RequestBody SpuAttrDto spuAttrDto){
        List<AttrDto> list = spuAttrDto.getAttrValueList();
        for(AttrDto a:list){
            Long attr_id = a.getAttr_id();
            String attr_value = a.getAttr_value();
            Attr attr = attrService.getById(attr_id);
            spuAttrValueService.save(new SpuAttrValue(null,spuAttrDto.getSpuId().longValue(),attr_id,attr.getAttrName(),attr_value,null,null));
        }
        return R.ok(null);
    }
}

